/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kasanova.sis.jsf;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.activation.DataHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.mail.util.ByteArrayDataSource;
import javax.servlet.ServletContext;
import kasanova.sis.jsf.util.JsfUtil;
import kasanova.sis.jsf.util.VerificationCode;
import kasanova.sis.repositorio.DataStorage;

import org.primefaces.event.FileUploadEvent;

@ManagedBean(name = "fileUpload")
@SessionScoped
public class FileUploadController implements Serializable {

    public static final String URL_RAIZ = "../imagens001/";
    private List<String> nomeImagens = new ArrayList<String>();
    private DataStorage dataStorage = new DataStorage();

    public FileUploadController() {
//        nomeImagens.add("imagem.jpg");
    }

    public void handleFileUpload(FileUploadEvent event) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ServletContext scontext = (ServletContext) facesContext.getExternalContext().getContext();
        String PASTA_RAIZ = scontext.getRealPath("");
        Boolean b = false;
        String nomeImagem;
        try {
            nomeImagem = nomeImagemRandomica(event.getFile().getContentType());
            nomeImagens.add(nomeImagem);
            b = uploadFile(PASTA_RAIZ, nomeImagem, event.getFile().getInputstream());
        } catch (IOException ex) {
            Logger.getLogger(FileUploadController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ContentTypeException ex) {
            Logger.getLogger(FileUploadController.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (b) {
            JsfUtil.addSuccessMessage(event.getFile().getFileName() + " enviada com sucesso!");
        } else {
            JsfUtil.addErrorMessage("Problemas no upload da sua imagem!");
        }
    }

    public List<String> getNomeImagens() {
        return nomeImagens;
    }

    /**
     *
     * @param pathDir
     * @param fileName
     * @param data
     * @param typeData
     * @return
     */
    public Boolean uploadFile(String pathDir, String fileName, byte[] data, String typeData) {
        return dataStorage.createFile(pathDir, fileName, new DataHandler(new ByteArrayDataSource(data, typeData)));
    }

    /**
     *
     * @param pathDir
     * @param fileName
     * @param in
     * @return
     */
    public Boolean uploadFile(String pathDir, String fileName, InputStream in) {
        return dataStorage.createFile(pathDir, fileName, in);
    }

    public String nomeImagemRandomica(String contentType) throws ContentTypeException {
        VerificationCode code = new VerificationCode();
        if (contentType.equals("image/jpeg")) {
            return code.toString() + ".jpg";
        } else if (contentType.equals("image/png")) {
            return code.toString() + ".png";
        } else if (contentType.equals("image/gif")) {
            return code.toString() + ".gif";
        }
        throw new ContentTypeException();
    }
}